package com.lvyq.javaprint.view.index;

import com.lvyq.javaprint.constant.ImgConstant;
import com.lvyq.javaprint.constant.TrayConstant;
import com.lvyq.javaprint.util.SettingUtils;
import com.lvyq.javaprint.view.style.STYLE;
import com.lvyq.javaprint.view.tray.TrayMainView;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.Random;

/**
 * @author lvyq
 * @version 1.0
 * Create by 2025-01-07 13:31
 */
public class IndexView {


    private static double  xOffset = 0;
    private static double yOffset = 0;


    /**
     * @description 创建首页内容
     * @author lvyq
     * @param[1] primaryStage
     * @throws
     * @time 2025-01-06 17:16
     */
    public static void createIndexView(Stage primaryStage) {
        primaryStage.initStyle(StageStyle.UNDECORATED);

        //创建拖动面板
        Pane dragPane =createDragPane();
        BorderPane root = new BorderPane();
        root.setLayoutX(TrayConstant.pane_Hor_Margin);
        root.setLayoutY(TrayConstant.pane_Vet_Margin);
        root.setPrefWidth(TrayConstant.stageW-TrayConstant.pane_Hor_Margin*2);
        root.setPrefHeight(TrayConstant.stageH-TrayConstant.pane_Vet_Margin*2);

        ImageView backGroundImg = SettingUtils.getBackGroundImg();
        //创建内容
        createCenterView(root,primaryStage, backGroundImg);
        dragPane.getChildren().addAll( backGroundImg,root);
        Scene scene = new Scene(dragPane, TrayConstant.stageW, TrayConstant.stageH,Color.TRANSPARENT);
        scene.getStylesheets().add("css/style.css");
        primaryStage.setScene(scene);
        primaryStage.setHeight(TrayConstant.stageH);
        primaryStage.setWidth(TrayConstant.stageW);
        //屏幕右小角
        rightBottomWindow(primaryStage);
        //关闭创建托盘
        TrayMainView.closeRequest(primaryStage);
    }

    /**
     * @description 窗口右下角
     * @author lvyq
     * @throws
     * @time 2024-04-24 15:58
     */
    public static void  rightBottomWindow(Stage primaryStage){
        //禁止改变窗口大小
        primaryStage.setResizable(false);
        // 获取主屏幕的宽度和高度
        Screen primaryScreen = Screen.getPrimary();
        double screenWidth = primaryScreen.getVisualBounds().getWidth();
        double screenHeight = primaryScreen.getVisualBounds().getHeight();
        // 设置窗口的初始位置为屏幕右下角
        primaryStage.setX(screenWidth - primaryStage.getWidth());
        primaryStage.setY(screenHeight - primaryStage.getHeight());

    }


    /**
     * @description  创建拖动面板
     * @author lvyq
     * @throws
     * @return Pane
     * @time 2025-01-07 16:10
     */
    private static Pane createDragPane() {
        Pane dragPane = new Pane();
        dragPane.setPrefHeight(TrayConstant.stageH);
        dragPane.setPrefWidth(TrayConstant.stageW);
        dragPane.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        });
        dragPane.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Stage stage = (Stage) ((Pane) event.getSource()).getScene().getWindow();
                stage.setX(event.getScreenX() - xOffset);
                stage.setY(event.getScreenY() - yOffset);
            }
        });

        return dragPane;
    }


    /**
     * @description 创建头部内容
     * @author lvyq
     * @throws
     * @time 2025-01-07 16:06
     */
    public  static void  createTopView( BorderPane root ,Stage primaryStage, ScrollPane scrollPane,VBox printView,BorderPane proxyView,ImageView backGroundImg,Button viewTitle  ){
        //上方
        HBox topBox = new HBox();
        topBox.setPrefHeight(20.00);
        topBox.setPrefWidth(TrayConstant.stageW-TrayConstant.pane_Hor_Margin*2);

        Button closeBut = new Button();
        closeBut.setGraphic(new ImageView(new Image(ImgConstant.INDEX_CLOSE)));

        Button proxyBut = new Button();
        proxyBut.setGraphic(new ImageView(new Image(ImgConstant.PROXY_SETTING)));


        Button pfBut = new Button();
        pfBut.setGraphic(new ImageView(new Image(ImgConstant.INDEX_PIFU)));

        topBox.setAlignment(Pos.CENTER_RIGHT);


        // 空白占位符
        Region spacer = new Region();
        topBox.setHgrow(spacer, Priority.ALWAYS);
        // 为按钮设置初始样式，并添加样式切换逻辑
        STYLE.BUTTON.toggle_style(closeBut,true);
        STYLE.BUTTON.toggle_style(proxyBut);
        STYLE.BUTTON.toggle_style(pfBut);
        closeBut.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                TrayMainView.toTrayView(primaryStage);
            }
        });
        //代理按钮事件
        ProxySettingView.proxyButFunction(proxyBut,scrollPane,proxyView,printView,viewTitle);

        //换肤事件
        pfButFunction(pfBut,backGroundImg);

        topBox.getChildren().addAll(viewTitle,spacer,pfBut,proxyBut,closeBut);
        root.setTop(topBox);

    }

    /**
     * @description
     * @author lvyq
     * @param[1] backGroundImg
     * @throws

     * @time 2025-01-10 9:01
     */
    private static void pfButFunction(Button pfBut,ImageView backGroundImg) {
        pfBut.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Random random = new Random();
                int randomNo = random.nextInt(5);
                 try {
                    String imgPath = "img/bg"+randomNo+".jpg";
                    //存储
                     SettingUtils.writeBackImgPath(imgPath);
                    Image newImage = new Image(imgPath); // 假设这是新的背景图路径
                    backGroundImg.setImage(newImage);
                    //刷新托盘的背景
                }catch (Exception e){
                    System.out.println("无图片>>>>>");
                    backGroundImg.setImage(null);
                }

            }
        });

    }

    /**
     * @description 创建主要内容
     * @author lvyq
     * @param[1] root
     * @param[2] primaryStage
     * @throws

     * @time 2025-01-07 16:15
     */
    public  static void   createCenterView( BorderPane root ,Stage primaryStage,ImageView backGroundImg){
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setPrefWidth(TrayConstant.stageW-TrayConstant.pane_Hor_Margin*2);
        scrollPane.setPrefHeight(TrayConstant.stageH-TrayConstant.pane_Vet_Margin*2-20.00);

        //创建打印列表内容视图
        VBox printView = PrintServerView.createPrintBoxView();

        Button viewTitle = new Button();
        //设置默认标头
        viewTitle.setText("打印服务列表");
        viewTitle.setStyle("-fx-background-color: rgba(0.0,0.0,0.0,0.0)");
        //设置默认内容
        scrollPane.setContent(printView);

        //创建代理设置视图
        BorderPane proxyView =ProxySettingView.createProxyBoxView(scrollPane,printView);

        //创建头部信息,切换内容
        createTopView(root,primaryStage,scrollPane,printView,proxyView,backGroundImg,viewTitle);

        root.setCenter(scrollPane);

    }





}
